/**
 * 开杠窗口
 * Created by duwei on 2018/4/26.
 */
var Ma_KaiGangWindow = Window.extend({
    blockBack: false,
    /**
     * 工程文件
     * @private
     */
    _ccsFileName :res.Ma_KaiGangWindow_json,
    initWindow: function (data, content, room) {
        this._super();
        this.data = data;
        this.content = content;
        this.room = room;
        this._initKaiGangWindow();
    },
    _initKaiGangWindow: function () {
        var self = this;
        self._nodeList['root.lb_content'].setString(self.content);
        var paiID = self.data['pai'];
        var paiIDA = self.data['pai_a'];
        var paiIDB = self.data['pai_b'];
        var qgh = self.data['qgh'];
        var opA = self.data['op_a'];
        var opB = self.data['op_b'];
        var uid = self.data['uid'];
        var row = self.room.uid2position[uid];

        var paiA = self._nodeList['root.a0'];
        var paiB = self._nodeList['root.a1'];
        paiA.setUserData({paiId: paiIDA});
        paiB.setUserData({paiId: paiIDB});
        setSpriteFrameByName(paiA, self.room.getPaiNameByRowAndId(2, paiIDA, true), majiang_hn.res.pai_hn_plist);
        setSpriteFrameByName(paiB, self.room.getPaiNameByRowAndId(2, paiIDB, true), majiang_hn.res.pai_hn_plist);

        var qghBtn = self._nodeList['root.btn_qgh'];
        qghBtn.setVisible(qgh);
        TouchUtil.setOnclickListener(qghBtn, function () {
            self.room.sendChiPengGang(self.room.opType.OP_HU, row, paiID);
        });
        var enableBtn = function (val, btn) {
            if (val) {
                Filter.remove(btn);
                TouchUtil.setOnclickListener(btn, function () {
                    var op = btn.getName().split('_')[0];
                    var idx = btn.getName().split('_')[1];
                    op = {chi: 1, peng: 2, gang: 50, bu: 3, hu: 4, guo: 0}[op];
                    var pai = btn.getParent().getChildByName('a' + idx);
                    var _paiId = pai.getUserData().paiId;
                    if (op == 1) {
                        self.room.sendChi(_paiId);
                    }
                    else {
                        self.room.sendChiPengGang(op, row, _paiId);
                    }
                });
            }
            else {
                Filter.grayScale(btn);
                TouchUtil.removeListeners(btn);
            }
        };

        enableBtn(opA[0], self._nodeList['root.chi_0']);
        enableBtn(opA[1], self._nodeList['root.peng_0']);
        enableBtn(opA[2], self._nodeList['root.gang_0']);
        enableBtn(opA[2], self._nodeList['root.bu_0']);
        enableBtn(opA[3], self._nodeList['root.hu_0']);

        enableBtn(opB[0], self._nodeList['root.chi_1']);
        enableBtn(opB[1], self._nodeList['root.peng_1']);
        enableBtn(opB[2], self._nodeList['root.gang_1']);
        enableBtn(opB[2], self._nodeList['root.bu_1']);
        enableBtn(opB[3], self._nodeList['root.hu_1']);

        TouchUtil.setOnclickListener(self._nodeList['root.guo'], function () {
            if (qgh) {
                showMessageBox("当前可以抢杠胡, 确定要【过】吗? \n(点击取消可重新选择抢杠胡)", function () {
                    self.room.sendChiPengGang(0, row, 0);
                });
            } else {
                self.room.sendChiPengGang(0, row, 0);
            }
        });
    },
});